using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Systems.Workflow.Enums;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Represents a workflow process within WFProcess
	/// </summary>
	public class Process
	{
		// Private fields for storing record fields //
		private int				_PK_ID = -1;
		private int				_FK_DefinitionID = -1;
		private string			_ProcessName;
		private WorkflowProcessType _ProcessType;
		private bool			_DataCanView = false;
		private bool			_DataCanEdit = false;
		private int				_Rank;

		// Public fields exposing record fields //
		public int PK_ID { get { return _PK_ID; } set { _PK_ID = value; } }
		public int FK_DefinitionID { get { return _FK_DefinitionID; } set { _FK_DefinitionID = value; } }
		public string ProcessName { get { return _ProcessName; } set { _ProcessName = value; } }
		public WorkflowProcessType ProcessType { get { return _ProcessType; } set { _ProcessType = value; } }
		public bool DataCanView { get { return _DataCanView; } set { _DataCanView = value; } }
		public bool DataCanEdit { get { return _DataCanEdit; } set { _DataCanEdit = value; } }
		public int Rank { get { return _Rank; } set { _Rank = value; } }

		// Linkbacks
		public Definition ParentDefinition
		{
			get
			{
				Definitions defs = new Definitions();
				return defs.GetByID( FK_DefinitionID );
			}
		}

		public ProcessStart [] ProcessStartActions
		{
			get
			{
				ProcessStarts ps = new ProcessStarts();
				return ps.GetAllProcessStartForProcessID( this.PK_ID );
			}
		}

		public Tasking[] ProcessTaskings
		{
			get
			{
				Taskings ts = new Taskings();
				return ts.GetTaskingsByProcessID( this.PK_ID );
			}
		}

		// Constructor to create new without loading
		public Process() { }

		// Constructor loading from a data row
		public Process(DataRow InitFromRow)
		{
			_PK_ID = SQLUtility.DBNull(InitFromRow["PK_ID"], -1);
			_FK_DefinitionID = SQLUtility.DBNull( InitFromRow["FK_DefinitionID"], -1 );
			_ProcessName = SQLUtility.DBNull(InitFromRow["ProcessName"], "");
			_ProcessType = (WorkflowProcessType) SQLUtility.DBNull(InitFromRow["ProcessType"], (int) WorkflowProcessType.PROCESS_CLEAR );
			_DataCanView = SQLUtility.DBNull(InitFromRow["DataCanView"], false);
			_DataCanEdit = SQLUtility.DBNull(InitFromRow["DataCanEdit"], false);
			_Rank = SQLUtility.DBNull(InitFromRow["Rank"], 1);
		}

		/// <summary>
		/// Adds a process start record to this process
		/// </summary>
		/// <param name="Method"></param>
		public void AddProcessStart(string Method)
		{
			if( this.PK_ID == -1 ) throw new Exception("Process record must be saved before adding a process start entry");

			ProcessStarts ps = new ProcessStarts();

			ProcessStart  p = new ProcessStart();
			p.FK_ProcessID = this.PK_ID;
			p.Rank = ps.GetNextRank( this.PK_ID );
			p.Method = Method;
			p.PropertyBag = "";

			ps.Save( p );
		}
	}
}
